class PasswordResetInstructionsController < ApplicationController
 
  before_filter :load_user_using_perishable_token, :only => [:edit, :update] 
  before_filter :require_no_user

  def new
    @page_title = 'Forgot password'    
  end

    def create
      @user = User.find_by_email(params[:email])
      if @user
	@user.reset_perishable_token!
	Notifier.password_reset_instructions(@user).deliver
	flash[:notice] = 'Instructions to reset your password have been sent to you. Please check your e-mail.'
	redirect_to :controller => 'user_session', :action => 'new'
      else
	@page_title = 'Forgot password'
	flash[:notice] = 'No user was found with that e-mail address.'
	render :action => :new
      end
   end
   
  def edit
    @page_title ='Cambiando contrasena'
  end

  def update
    @user.password = params[:user][:password]
    @user.password_confirmation = params[:user][:password_confirmation]
    if @user.save
      flash[:notice] = "Password successfully updated"
      redirect_to :controller =>'user', :action => 'show'
    else
      render :action => :edit
    end
  end

  private
    def load_user_using_perishable_token      
      @user = User.find_using_perishable_token(params[:id])
      unless @user
        flash[:notice] = "We're sorry, but we could not locate your account." +
          "If you are having issues try copying and pasting the URL " +
          "from your email into your browser or restarting the " +
          "reset password process."
        redirect_to :controller => 'user_session', :action => 'new'
      end
    end
   
end
